<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

class ot_shipping {

	function ot_shipping() {
		$this->module_key = 'MODULE_ORDER_TOTAL_SHIPPING';
		$this->code = 'ot_shipping';
		$this->enabled = ((MODULE_ORDER_TOTAL_SHIPPING_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER;
	}

	function process($params, &$checkout_component)
	{
		$wholesale = $checkout_component->accountInfo['wholesale'];
		$cart = &$checkout_component->cart;
		$cart_prices = &$checkout_component->cart_prices;
		$shippings = &$checkout_component->shipping;
		
		reset($shippings);
		while (list(,$shipping) = each($shippings))
		{
			$module = $shipping['module'];
			if ($module == $checkout_component->shipping_module)
			{
				$weight = $shipping['weight'];
				$num_boxes = $shipping['num_boxes'];
				$handling = $shipping['handling'];
				if ($wholesale) $tax_rate = 0;
				else $tax_rate = $shipping['tax_rate'];
				$rates = $shipping['rates'];

				while (list(,$rate) = each($rates))
				{
					$service = $rate['service'];
					if ($service == $checkout_component->shipping_service)
					{
						$price = ($rate['postage'] + $handling) * $num_boxes;
						if ($price < 0.000001) {
							$cart_prices[$this->code] = array('value' => $price, 'title' => 'Free Shipping:', 'class' => $this->code);
						}
						else {
							$cart_prices[$this->code] = array('value' => $price, 'title' => $module .' ('.$num_boxes.' x '.$weight.'lbs) ('.$service.'):', 'class' => $this->code);
						
						}
						$tax = 0.0;
						if ($tax_rate > 0) {
							$cart_prices['ot_tax']['value'] += $price * ($tax_rate /100);
						}
					}
				}
			}
		}
	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Display Coupon', 'MODULE_ORDER_TOTAL_SHIPPING_STATUS', 'true', 'Do you want to display the shipping?', '6', '1','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER', '30', 'Sort order of display.', '6', '2', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}
}
?>
